// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.IO;
using System.Reflection;
using Microsoft.Win32;
using svs.Mobile.BeyondMedia.FeedCore;
using svs.Mobile.BeyondMedia.Objects;

namespace svs.Mobile.BeyondMedia.Controls.Players.WindowsMediaPlayer
{
	public delegate void SkinChangedEvent(ISkin i_skin);


	internal class WMPSkinManager : SkinManagerBase, ISkinManager
	{
		private const string MEDIAPLAYER_SKIN_PATH = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MediaPlayer\Skins";
		private const string MEDIAPLAYER_SKIN_KEY = "OCXSkin";
		private static SkinList m_Skins = new SkinList();
		private static ISkin m_CurrentSkin = null;

		public event SkinChangedEvent SkinChanged;

		private SkinList GetDirectorySkins(DirectoryInfo i_directory)
		{
			SkinList l = new SkinList();
			FileInfo[] skins = i_directory.GetFiles("*.skn");
			foreach (FileInfo f in skins)
			{
				WMPSkin skin = new WMPSkin(f, f.FullName == CurrentPlayerSkin.SkinPath);
				
				if(MatchesDeviceScreenOrientation(skin))
					l.Add(skin);
			}

			return l;
		}


		/// <summary>
		/// SkinPath to the current media player skin
		/// </summary>
		public ISkin CurrentPlayerSkin
		{
			get
			{
				if (m_CurrentSkin == null)
				{
					string skin = Registry.GetValue(MEDIAPLAYER_SKIN_PATH, MEDIAPLAYER_SKIN_KEY, "") as String;
					m_CurrentSkin = new WMPSkin(null, skin, true);
				}

				return m_CurrentSkin;
			}
			set
			{
				if (!m_CurrentSkin.Equals(value))
				{
					Registry.SetValue(MEDIAPLAYER_SKIN_PATH, MEDIAPLAYER_SKIN_KEY, value.SkinPath);
					if (SkinChanged != null)
						SkinChanged(value);

					m_CurrentSkin = value;
				}
			}
		}

		/// <summary>
		/// SkinPath to the original (for Windows) player skin
		/// </summary>
		public ISkin OriginalPlayerSkin
		{
			get
			{
				string original = Configuration.OriginalPlayerSkinPath;
				if (original == null || original.Length == 0)
				{
					OriginalPlayerSkin = CurrentPlayerSkin;
					return CurrentPlayerSkin;
				}
				else
				{
					return new WMPSkin("Default", original, false);
				}
			}
			set
			{
				Configuration.OriginalPlayerSkinPath = value.SkinPath;
				Configuration.SaveSettings();
			}
		}

		/// <summary>
		/// Returns a list of all installed skins
		/// </summary>
		public SkinList InstalledSkins
		{
			get
			{
				if (m_Skins.Count == 0)
				{
					m_Skins.Add(OriginalPlayerSkin);

					string skinsFolder = Path.GetDirectoryName(
						Assembly.GetExecutingAssembly().GetName().CodeBase);
					skinsFolder += @"\Skins";

					DirectoryInfo skinsInfo = new DirectoryInfo(skinsFolder);
					if (skinsInfo.Exists)
					{
						DirectoryInfo[] skinDirs = skinsInfo.GetDirectories();

						foreach (DirectoryInfo di in skinDirs)
						{
							m_Skins.AddRange(GetDirectorySkins(di));
						}
					}

					if (!m_Skins.Contains(CurrentPlayerSkin))
						m_Skins.Add(CurrentPlayerSkin);
					
				}

				return m_Skins;
			}
		}
		
	}
}